package dao;

import common.User;
import service.ControlSCConnectionThread;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

public class GetStu {
    private static Connection conn=DataBaseConnect.getConn();
    private static String sql="select id,name,number,phone,birthdate from account where id in(select studentid from student_course where courseid=?)";
    private static PreparedStatement pstmt;
    static {
        try {
            pstmt=conn.prepareStatement(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public static Vector<User> gatStu(String courseid){
        Vector<User> student=new Vector<>();
        try {
            pstmt.setString(1,courseid);
            ResultSet rs=pstmt.executeQuery();
            while(rs.next()){
                User user=new User(rs.getString(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5));
                if(ControlSCConnectionThread.getHashMap().get(user.getId())!=null){
                    user.setState(User.onLine);
                }
                student.add(user);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return student;
    }
}
